package com.wzdigit.wms.wms.controller.inventory;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.basic.SearchGoodsTempRequest;
import com.wzdigit.wms.basic.client.response.basic.GoodsTempResponse;
import com.wzdigit.wms.basic.client.response.basic.ImportGoodsTempResponse;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.service.core.inventory.GoodsTempService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 货品临时表控制器
 * Created by zyf on 2021/5/17 13:38
 */
@RestController
@Api(tags = "临时货品")
@RequestMapping(value = WmsWareConstant.API_VERSION)
public class GoodsTempController {
    @Autowired
    private GoodsTempService goodsTempService;

    private static final String REQUEST_MODEL = "/goodsTemp";

    /**
     * 导入《临时货品信息》
     * @param file
     * @param servletRequest
     * @param servletResponse
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "导入《临时货品信息》", notes = "导入《临时货品信息》")
    @ApiResponses({@ApiResponse(code = 500, message = "系统异常", response = ErrorEntity.class)})
    @PostMapping(value = REQUEST_MODEL + "/importGoodsTemp")
    public ImportGoodsTempResponse importGoodsTemp(MultipartFile file,
                                                 HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
        try {
            return goodsTempService.importGoodsTemp(file,servletRequest,servletResponse);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "获取货品中间信息（分页）", notes = "获取货品中间信息（分页）")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = REQUEST_MODEL + "/SaleInv", method = RequestMethod.GET)
    public PagerInfo<List<GoodsTempResponse>> searchGoodsTemp(
            SearchGoodsTempRequest search, Pager<?> pager,
            HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
        try {
            return goodsTempService.searchGoodsTemp(search,pager);
        } catch (Exception ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }
}
